/////////////////////////////////////////////////////////////////////////////
// Name:        valgenvariant.cpp
// Purpose:     wxGenericVariantValidator class
// Author:      Julian Smart
// Modified by:
// Created:     July 27th 2007
// RCS-ID:      $Id: valgenvariant.cpp,v 1.1 2007/07/31 21:24:53 anthemion Exp $
// Copyright:   (c) Julian Smart
// Licence:     New BSD License
/////////////////////////////////////////////////////////////////////////////

#include "wx/wx.h"

#if wxUSE_VALIDATORS

#include "variantutils/valgenvariant.h"

IMPLEMENT_CLASS(wxGenericVariantValidator, wxValidator)

wxGenericVariantValidator::wxGenericVariantValidator(wxVariant* variant): wxGenericValidator((wxString*) NULL)
{
    Init();

    m_variant = variant;

    InitializeFromVariant();
}

wxGenericVariantValidator::wxGenericVariantValidator(const wxGenericVariantValidator& val)
    : wxGenericValidator(val)
{
    Init();

    Copy(val);

    InitializeFromVariant();
}

bool wxGenericVariantValidator::Copy(const wxGenericVariantValidator& val)
{
    wxGenericValidator::Copy(val);

    m_variant = val.m_variant;

    return true;
}

// Called to transfer data to the window
bool wxGenericVariantValidator::TransferToWindow(void)
{
    if (m_variant)
    {
        wxString type(m_variant->GetType());
        wxASSERT( !type.IsEmpty() );
        
        if (type == wxT("string"))
        {
            m_string = m_variant->GetString();
        }
        else if (type == wxT("long"))
        {
            m_int = (int) m_variant->GetLong();
            return true;
        }
        else if (type == wxT("bool"))
        {
            m_bool = m_variant->GetBool();
            return true;
        }
        else
        {
            wxASSERT_MSG( false, wxT("Unsupported variant type"));
            return false;
        }
    }

    return wxGenericValidator::TransferToWindow();
}

// Called to transfer data from the window
bool wxGenericVariantValidator::TransferFromWindow(void)
{
    if (!wxGenericValidator::TransferFromWindow())
        return false;

    if (m_variant)
    {
        wxString type(m_variant->GetType());
        wxASSERT( !type.IsEmpty() );
        
        if (type == wxT("string"))
        {
            (*m_variant) = m_string;
        }
        else if (type == wxT("long"))
        {
            (*m_variant) = (long) m_int;
        }
        else if (type == wxT("bool"))
        {
            (*m_variant) = m_bool;
        }
        else
        {
            wxASSERT_MSG( false, wxT("Unsupported variant type"));
            return false;
        }
    }
    else
        return false;

    return true;
}

// Initialize
void wxGenericVariantValidator::Init()
{
    m_variant = NULL;
    m_int = 0;
    m_bool = false;
}


bool wxGenericVariantValidator::InitializeFromVariant()
{
    if (m_variant && !m_variant->IsNull())
    {
        wxString type(m_variant->GetType());
        wxASSERT( !type.IsEmpty() );
        
        if (type == wxT("string"))
        {
            m_pString = & m_string;
            return true;
        }
        else if (type == wxT("long"))
        {
            m_pInt = & m_int;
            return true;
        }
        else if (type == wxT("bool"))
        {
            m_pBool = & m_bool;
            return true;
        }
        else
        {
            wxASSERT_MSG( false, wxT("Unsupported variant type"));
            return false;
        }
    }
    return false;
}

#endif
  // wxUSE_VALIDATORS
